package com.h3c.ptability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.h3c.ptability.config.SpringUtil;
import com.h3c.ptability.entity.PtEsignRecord;
import com.h3c.ptability.enums.ElectronicSignRecordInfoStatus;
import com.h3c.ptability.enums.ElectronicSignRecordStatus;
import com.h3c.ptability.mapper.PtEsignRecordInfoMapper;
import com.h3c.ptability.entity.PtEsignRecordInfo;
import com.h3c.ptability.service.IPtEsignRecordInfoService;
import com.h3c.ptability.service.IPtEsignRecordService;
import com.h3c.ptability.utils.MqUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现类
 *
 * @author chenyifan
 * @since 2022-05-20 09:24:54
 */
@Service
@AllArgsConstructor
public class PtEsignRecordInfoServiceImpl extends ServiceImpl<PtEsignRecordInfoMapper, PtEsignRecordInfo> implements IPtEsignRecordInfoService {


    @Override
    public List<PtEsignRecordInfo> getRecordInfosByRecordId(String recordId) {
        return list(new QueryWrapper<PtEsignRecordInfo>().lambda()
                .eq(PtEsignRecordInfo::getRecordId, recordId)
        );
    }

    @Override
    public void updateRecordInfoFinish(String fileId, String docFileKey, Boolean isWaste) {
        PtEsignRecordInfo recordInfo;
        if (isWaste) {
            recordInfo = getOne(new QueryWrapper<PtEsignRecordInfo>().lambda()
                    .eq(PtEsignRecordInfo::getCancelDocFileKey, docFileKey));
        } else {
            recordInfo = getOne(new QueryWrapper<PtEsignRecordInfo>().lambda()
                    .eq(PtEsignRecordInfo::getUserDocFileKey, docFileKey));
        }
        if (isWaste) {
            recordInfo.setCancelTime(new Date());
            recordInfo.setStatus(ElectronicSignRecordInfoStatus.WASTE_SIGN_PROCESS);
        } else {
            recordInfo.setFinishTime(new Date());
            recordInfo.setStatus(ElectronicSignRecordInfoStatus.SIGN_SUCCESS);
        }
        recordInfo.setSfsId(fileId);
        updateById(recordInfo);
        List<PtEsignRecordInfo> recordInfos = list(new QueryWrapper<PtEsignRecordInfo>().lambda()
                .eq(PtEsignRecordInfo::getRecordId, recordInfo.getRecordId())
        );
        List<PtEsignRecordInfo> successRecordInfos;
        if (isWaste) {
            successRecordInfos = recordInfos.stream()
                    .filter(s -> s.getStatus().equals(ElectronicSignRecordInfoStatus.WASTE_SIGN_PROCESS))
                    .collect(Collectors.toList());
        } else {
            successRecordInfos = recordInfos.stream()
                    .filter(s -> s.getStatus().equals(ElectronicSignRecordInfoStatus.SIGN_SUCCESS))
                    .collect(Collectors.toList());
        }
        if (successRecordInfos.size() == recordInfos.size()) {
            PtEsignRecord record = SpringUtil.getBean(IPtEsignRecordService.class).getById(recordInfo.getRecordId());
            if (isWaste) {
                record.setFinishCancelTime(new Date())
                        .setSignStatus(ElectronicSignRecordStatus.WASTE_SIGN_FINISH);
                SpringUtil.getBean(IPtEsignRecordService.class).updateById(record);
                if(record.getSceneType().equals(1)){
                    MqUtils.pushSignStatusToNem(record.getWorkerId(), MqUtils.WAIT_SIGN_CODE);
                } else if (record.getSceneType().equals(PtEsignRecord.TALENT_SCENE_TYPE)) {
                    MqUtils.pushSignStatusToNemTalent(record.getWorkerId(), MqUtils.TALENT_CANCEL_SIGN, "");
                }
            } else {
                record.setSignFinishTime(new Date())
                        .setSignStatus(ElectronicSignRecordStatus.SIGN_SUCCESS);
                SpringUtil.getBean(IPtEsignRecordService.class).updateById(record);
                if(record.getSceneType().equals(PtEsignRecord.ON_BOARDING_SCENE_TYPE)){
                    MqUtils.pushSignStatusToNem(record.getWorkerId(), MqUtils.NEM_SIGN_SUCCESS_CODE);
                }else if(record.getSceneType().equals(PtEsignRecord.TALENT_SCENE_TYPE)){
                    MqUtils.pushSignStatusToNemTalent(record.getWorkerId(), MqUtils.TALENT_ALREADY_SIGN,"");
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRecordInfoFinish(String fileId, String recordId, String templateId) {
        PtEsignRecordInfo recordInfo = getOne(new QueryWrapper<PtEsignRecordInfo>().lambda()
                .eq(PtEsignRecordInfo::getRecordId, recordId)
                .eq(PtEsignRecordInfo::getTemplateId, templateId)
        );
        recordInfo.setStatus(ElectronicSignRecordInfoStatus.SIGN_SUCCESS);
        recordInfo.setSfsId(fileId);
        recordInfo.setFinishTime(new Date());
        updateById(recordInfo);
        List<PtEsignRecordInfo> recordInfos = list(new QueryWrapper<PtEsignRecordInfo>().lambda()
                .eq(PtEsignRecordInfo::getRecordId, recordInfo.getRecordId())
        );
        List<PtEsignRecordInfo> successRecordInfos = recordInfos.stream()
                .filter(s -> s.getStatus().equals(ElectronicSignRecordInfoStatus.SIGN_SUCCESS))
                .collect(Collectors.toList());
        if (successRecordInfos.size() == recordInfos.size()) {
            PtEsignRecord record = SpringUtil.getBean(IPtEsignRecordService.class).getById(recordInfo.getRecordId());
            SpringUtil.getBean(IPtEsignRecordService.class).updateById(
                    record
                            .setSignFinishTime(new Date())
                            .setSignStatus(ElectronicSignRecordStatus.SIGN_SUCCESS)
            );
            MqUtils.pushSignStatusToNem(record.getWorkerId(), MqUtils.NEM_SIGN_SUCCESS_CODE);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRecordInfoFail(String docFileKey) {
        PtEsignRecordInfo recordInfo = getOne(new QueryWrapper<PtEsignRecordInfo>().lambda()
                .eq(PtEsignRecordInfo::getUserDocFileKey, docFileKey)
        );
        recordInfo.setStatus(ElectronicSignRecordInfoStatus.SIGN_FAIL);
        updateById(recordInfo);
        PtEsignRecord record = SpringUtil.getBean(IPtEsignRecordService.class).getById(recordInfo.getRecordId());
        if (!record.getSignStatus().equals(ElectronicSignRecordStatus.SIGN_FAIL)) {
            SpringUtil.getBean(IPtEsignRecordService.class).updateById(new PtEsignRecord()
                    .setRecordId(recordInfo.getRecordId())
                    .setSignStatus(ElectronicSignRecordStatus.SIGN_FAIL)
            );
            MqUtils.pushSignStatusToNem(record.getWorkerId(), MqUtils.NEM_SIGN_FAIL_CODE);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRecordInfoFail(String recordId, String templateId) {
        update(null, new LambdaUpdateWrapper<PtEsignRecordInfo>()
                .set(PtEsignRecordInfo::getStatus, ElectronicSignRecordInfoStatus.SIGN_FAIL)
                .eq(PtEsignRecordInfo::getRecordId, recordId)
                .eq(PtEsignRecordInfo::getTemplateId, templateId)
        );
        PtEsignRecord record = SpringUtil.getBean(IPtEsignRecordService.class).getById(recordId);
        if (!record.getSignStatus().equals(ElectronicSignRecordStatus.SIGN_FAIL)) {
            SpringUtil.getBean(IPtEsignRecordService.class).updateById(new PtEsignRecord()
                    .setRecordId(recordId)
                    .setSignStatus(ElectronicSignRecordStatus.SIGN_FAIL)
            );
            MqUtils.pushSignStatusToNem(record.getWorkerId(), MqUtils.NEM_SIGN_FAIL_CODE);
        }
    }
}

